var vm = new Vue({
    el: '#big',
    delimiters: ['[[', ']]'], // 修改vue模板符号，防止与django冲突
    data: {
        host: host,
        username: sessionStorage.username || localStorage.username,
        user_id: sessionStorage.user_id || localStorage.user_id,
        avatar_url: sessionStorage.avatar_url || localStorage.avatar_url,
        token: sessionStorage.token || localStorage.token,
        nick_name: sessionStorage.nick_name || localStorage.nick_name,
        id: '',
        answers: [],
        quest_id: '',
        create_time: '',
        title: '',
        label: '',
        answer_count: '',
        new_time: '',
        answer: '',
        user: '',
        contents: [],
        content: '',
        text: '',
        habbit: [],  // 点赞 举报 踩
        lbques: [],   // 标签下面的所有问题
        label_id:'',
    },

    //由于后台静态化，故将刷新界面的请求忽视掉
    mounted: function () {
    //     this.id = this.get_query_string('id')
    //     axios.get(host + "/answers/" + this.id)
    //
    //         .then(function (response) {
    //             // console.log(response.data)
    //             // vm.contents = response.data;
    //             vm.ques_id = response.data.id;
    //             let label_id = response.data.label.id;
    //             // console.log(vm.ques_id)
    //
    //             alert(response.data.label.id)
    //             axios.get(host + "/answers/LabelQuest/?label_id=" + label_id, {
    //                 headers: {
    //                     // 后台接收的JWT对格式有要求,'JWT'开头,空一格拼接字符串
    //                     'Authorization': 'JWT ' + vm.token
    //                 },
    //                 responseType: 'json',
    //             })
    //
    //                 .then(function (response) {
    //                     //console.log(response.data)
    //                     vm.lbques = response.data;
    //
    //
    //                     console.log(vm.lbques)
    //                 });
    //         });


        axios.get(host + "/answers/maincomments/user/",{
            headers:{
                'Authorization': 'JWT ' + this.token
            },
            responseType:'json',
        })

            .then(function (response) {
                // console.log(response.data)
                vm.habbit = response.data.is_report;

                // console.log(vm.ques_id)


            })


    },
    filters:{
        time_change:function (value) {

        }
    },
    methods: {
        logout: function () {
            sessionStorage.clear();
            localStorage.clear();
            location.href = './person-loginsign.html';
        },
        // 获取url路径参数
        get_query_string: function (name) {
            var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
            var r = window.location.search.substr(1).match(reg);
            if (r != null) {
                return decodeURI(r[2]);
            }
            return null;
        },

        // 点击页数
        on_page: function (num) {
            if (num != this.page) {
                this.page = num;
                this.get_spits();
            }
        },
        // 时间戳
        getDateDiff: function (value) {
            Time = new Date(value).getTime();
            var minute = 1000 * 60;
            var hour = minute * 60;
            var day = hour * 24;
            var halfamonth = day * 15;
            var month = day * 30;
            var now = new Date().getTime();
            var diffValue = now - Time;
            if (diffValue < 0) {
                return;
            }
            var monthC = diffValue / month;
            var weekC = diffValue / (7 * day);
            var dayC = diffValue / day;
            var hourC = diffValue / hour;
            var minC = diffValue / minute;
            if (monthC >= 1) {
                result = "" + parseInt(monthC) + "月前";
            }
            else if (weekC >= 1) {
                result = "" + parseInt(weekC) + "周前";
            }
            else if (dayC >= 1) {
                result = "" + parseInt(dayC) + "天前";
            }
            else if (hourC >= 1) {
                result = "" + parseInt(hourC) + "小时前";
            }
            else if (minC >= 1) {
                result = "" + parseInt(minC) + "分钟前";
            } else
                result = "刚刚";
            return result;
        },

        shijianchuo: function (Time) {
            var b = new Date(Time)
            return b.getTime()
        },


        pinlun: function () {
            $(".comment").click(function () {
                $(this).parents(".operate").siblings(".comment-box").toggle();
            });
            $(".sidebar .fa").hover(function () {
                $(this).css("color", "#e64620");
            }, function () {
                $(this).css("color", "#000");
            })
        },

        dianzan: function (type_id, content_id) {

            $.ajax({
                url: host + '/answers/maincomments/operate/',
                type: 'POST',
                dataType: 'json',
                data: {
                    type_id: type_id,

                    content_id: content_id,

                    'user_id': this.user_id,


                },
                success: function (dat) {
                    window.location.reload(); //刷新当前页面
                    //$('#operate').html(dat)
                },
                error: function () {
                    alert('点赞失败');
                }
            });


        },


        tijiao: function () {

            $.ajax({
                url: host + '/answers/maincomments/',
                type: 'POST',
                dataType: 'json',
                data: {
                    'content': this.text,
                    'ques_id': this.ques_id,
                    'user_id': this.user_id,


                },
                success: function (dat) {
                    alert('评论成功')
                    window.location.reload(); //刷新当前页面
                    // $('#answer-intro').location.reload()

                },
                error: function () {
                    alert('提交失败');
                }
            });


        }


    }
    ,


});


$(function () {
    // 打开登录框
    $('.comment_form_logout').click(function () {
        $('.login_form_con').show();
    })
})




